/**
 *   商品套装JS
 *   上海百胜软件有限公司
 *   网站地址: http://baison.com.cn;
 *   author:  fillbag  286116033
 */

var GroupPromotion = 
{
	
    filename : location.href.substring((location.href.lastIndexOf("/")) + 1, location.href.lastIndexOf("?")), 
	errMsg : new Array(), 	 
	_self : this,
	float : {
		containerBack : 0,
		containerTop : 0,
	    switchColorImg : 0
	},
	floating : function (){
		var containerName = "float-container-" + (Math.random()*10) + new Date().getTime();
		this.float = {
			"containerBack" : 'backDiv',
			"containerTop" : 'topDiv',
			"ImgSwitchDefaultImg" : './images/switch_color_defaultimg.jpg'
	    };
	},	
	_resize:function(){
		var id = this.float.containerTop;
		sroll = GroupPromotion.getScroll();
		if (document.getElementById(id)){
			div = document.getElementById(id)
			div.style.left=parseFloat(window.screen.width)/2-300+"px";
			div.style.top =sroll.t + 40 + "px";
		}
	},
	getScroll : function ()  {     
		var t, l, w, h;          
		if (document.documentElement && document.documentElement.scrollTop){         
			t = document.documentElement.scrollTop;         
			l = document.documentElement.scrollLeft;         
			w = document.documentElement.scrollWidth;         
			h = document.documentElement.scrollHeight;     
		} else if (document.body) {         
			t = document.body.scrollTop;         
			l = document.body.scrollLeft;         
			w = document.body.scrollWidth;         
			h = document.body.scrollHeight;     
		}     
		return { t: t, l: l, w: w, h: h }; 
	},
	showDiv : function (data){
		this.floating();
		var id = this.float.containerBack;
		if ( ! document.getElementById(id)){
			var bodyheight = document.body.scrollHeight;
			var screenheight = window.screen.availHeight;
			var divheight = bodyheight;
			if (screenheight > bodyheight)
				divheight = screenheight;				
			div = document.createElement("DIV");
			div.id = id;
			div.style.position = "absolute";
			div.style.top = "0px";
			div.style.left = "0px";
			div.style.width = document.body.scrollWidth + "px";
			div.style.height = divheight + "px";			
			div.style.backgroundColor = "#ffffff";
			div.style.filter = "Alpha(opacity=80);opacity:0.8";
			div.style.zIndex = "999";
			document.body.appendChild(div);
		}else{
			document.getElementById(id).style.display="";
		}

		var id = this.float.containerTop;
		if(topDiv = document.getElementById(id)){
			topDiv.parentNode.removeChild(topDiv);
		}
		if ( ! document.getElementById(id)){	
			sroll = GroupPromotion.getScroll();
			div = document.createElement("DIV");
			div.id = id;
			div.style.position = "absolute";
			div.style.width = "600px";
			div.style.height = "527px";	
			div.style.left=parseFloat(window.screen.width)/2-300+"px";			
			div.style.top = sroll.t + 40 + "px";
			div.style.backgroundColor = "#ffffff";
			div.style.border = "#666666 1px solid";
			div.style.overflow = "hidden";
			div.style.zIndex = "999";		   
			document.body.appendChild(div);
			div.innerHTML = data;
		}
		return;
	},
	loadDetailResponse : function (result){
	    if (!result.error){
			GroupPromotion.showDiv(result.html);
		}

		if (result.message.length > 0){
		  alert(result.message);
		}
		return;
	},	
	show : function (type_id, value_id){
		var filter = new Object;	
		filter.type = type_id;
		filter.value = value_id;
		//Ajax.debug(1,0); 
		Ajax.call(this.filename + "?act=goods_detail", filter, this.loadDetailResponse, "GET", "JSON");
	}, 
	setGoodsSize : function(e,size_id, size_name){
		var elements = document.forms['gp_form'].elements; 	
		
		elements['size_id'].value = size_id;		
		elements['size_name'].value = size_name;
		
		var sizes  = document.getElementsByName('goods_size_display[]');
		for(i=0;i<sizes.length;i++){
			sizes[i].parentNode.className = "";
		}
		e.className = "current";
		
		var e  = document.getElementById('current_goods_size1');
		e.innerHTML = size_name;
		
		e  = document.getElementById('current_goods_size2');
		e.innerHTML = size_name;
	},
    setGoodsColor : function (e,color_id, color_name){
		
		var elements = document.forms['gp_form'].elements; 	
		var switchColorImg = elements['switch_color_img'].value;
		
		elements['color_id'].value = color_id;		
		elements['color_name'].value = color_name;

		
		var colors  = document.getElementsByName('goods_color_display[]');
		for(i=0;i<colors.length;i++){
			colors[i].parentNode.className = "";
		}
		e.className = "current";	
		var e  = document.getElementById('current_goods_color1')
		e.innerHTML = color_name;
		
		e  = document.getElementById('current_goods_color2')
		e.innerHTML = color_name;
		if(Utils.isInt(color_id) && switchColorImg){

			var filter = new Object;
			filter.goods_id = elements['goods_id'].value;
			filter.color_id = color_id;
			//Ajax.debug(1,0);
			Ajax.call(this.filename + '?act=switch_color_img', filter, this.loadImgSwitchConsole, 'GET', 'JSON');  
		} 
	},	
	loadImgSwitchConsole : function (res){		
		var defaultImg = GroupPromotion.float.ImgSwitchDefaultImg; //默认图片路径
		var gg_list = res.result.list;

		var g_pics = document.getElementById('goods_pics');
		var t_c = document.getElementById('show_top_case');
		var gg_viewpic = document.getElementById('gg_viewpic');
		var targetObj = document.getElementById('demo1');
		if(gg_list && gg_list.length>0){
			var ul = document.createElement('ul');
			
			removeChilds(targetObj);
			
			targetObj.appendChild(ul);
			if(gg_list[0].img_id>0){
			
				removeChilds(t_c);
				
				var t_a = document.createElement('a');
				t_a.setAttribute('href',gg_list[0].img_original);
				t_a.className = 'MagicZoom';
				t_c.appendChild(t_a);
				t_c.innerHTML = '<img src="'+gg_list[0].img_url+'" alt="'+res.result.goods_name+'" onload="setImgSize(this,230,230)"/>';
			}
			for(var i=0;i<gg_list.length;i++){
				var li = document.createElement('li');								
				li.style.float='left';
				ul.appendChild(li);
				
				var a = document.createElement('a');
				a.setAttribute('href','gallery.php?id='+gg_list[i].goods_id+'&img='+gg_list[i].img_id);
				a.setAttribute('target','_blank');				
				li.appendChild(a);
				
				var img = document.createElement('img');
				img.setAttribute('src',Utils.trim(gg_list[i].thumb_url) != '' ? gg_list[i].thumb_url : gg_list[i].img_url);
				img.setAttribute('alt',res.result.goods_name);
				a.appendChild(img);	
			}
		}else{
			var ul_obj = document.getElementById('ul_obj');
			ul_obj.style.display = 'none';
			gg_viewpic.style.display = "none";
			g_pics.removeChild(t_c);
			var n_tc = document.createElement('p');
			n_tc.setAttribute('id','show_top_case');
			g_pics.appendChild(n_tc);
			var t_a = document.createElement('a');
			t_a.setAttribute('href','#');
			t_a.className = 'MagicZoom';
			n_tc.appendChild(t_a);
			var t_img = document.createElement('img');
			t_img.setAttribute('src',defaultImg);
			t_img.setAttribute('alt','暂无图片');
			t_a.appendChild(t_img);
		}		
	},
    hideDiv : function(){
		document.getElementById(this.float.containerBack).style.display="none";
		document.getElementById(this.float.containerTop).style.display="none";
    },
    selected : function(){
    	var elements = document.forms['gp_form'].elements; 	
    	var filter = new Object;
		filter.type = elements['group_pro_goods_type'].value;
		filter.goods_id = elements['goods_id'].value;
		filter.color_id = elements['color_id'].value;
		filter.size_id = elements['size_id'].value;
	    this.checkV(filter);
        if(this.passed()){
        	Ajax.call(this.filename + "?act=add_to_tmp_cart", filter, this.tmpCartList, "GET", "JSON");
        }	    
    },
    tmpCartList : function(result){
    	if (!result.error){  
       		itemCartHTML = Utils.$('tmp_cart_list').innerHTML;       		
       		itemCartHTML += result.html;
       		Utils.$('tmp_cart_list').innerHTML = itemCartHTML;
			GroupPromotion.hideDiv();
    	}   
		if (result.error.length > 0){
		  alert(result.error);
		  return;
		}
		return;
    },
    checkV : function (filters){
		if(!Utils.isInt(filters.type) || !Utils.isInt(filters.goods_id)){
			this.addErrorMsg('参数错误！');
		}		
		
		if(!Utils.isNumber(filters.color_id) ){
			this.addErrorMsg('请设置商品颜色！');
		}
		if(!Utils.isNumber(filters.size_id) ){
			this.addErrorMsg('请设置商品尺码！');
		}
	
		var gpg_type = document.getElementsByName('group_pro_goods_type[]');
		var g_ids = document.getElementsByName('goods_ids[]');
		var c_ids = document.getElementsByName('color_ids[]');
		var s_ids = document.getElementsByName('size_ids[]');
		var check_count = Utils.$('group_pro_goods_type_'+filters.type+'_count').value;

	    var t_count = 0;

	    if(gpg_type.length>0){
			for(var i=0;i<gpg_type.length;i++){	
				if(gpg_type[i].value==filters.type){
					t_count += 1;
				}
			}
			if(t_count==check_count){
		    	this.addErrorMsg('该商品套装类型下商品你已经选满！');
			}	
	    }
    },

    del : function(targetObj){
    	Utils.$('tmp_cart_list').removeChild(targetObj.parentNode.parentNode.parentNode.parentNode); 	
    },

	passed : function(){
		if (this.errMsg.length > 0)	{
			var msg = "";
			for (i = 0; i < this.errMsg.length; i ++ ){
				msg += "- " + this.errMsg[i] + "\n";
			}		
			alert(msg);
			this.errMsg.length = 0;
			return false;
		}else{
		  	return true;
		}
	},
    addErrorMsg : function(str){
        this.errMsg.push(str);
  	},
	getGroupPros : function(){
		var arr_g_ids = document.getElementsByName('goods_ids[]');
		var gpg_ext_type  = document.getElementsByName('group_pro_g_ext_type[]');
		var gpg_type  = document.getElementsByName('group_pro_goods_type[]');
		var arr_c_ids = document.getElementsByName('color_ids[]');
		var arr_s_ids = document.getElementsByName('size_ids[]');
		var all_count =  parseInt(document.getElementById('group_pro_count').value);
		var ext_money =  0.00;
		if(gpg_ext_type && gpg_ext_type.length>0){
			for(var i=0;i<gpg_ext_type.length;i++){
				var exsits = false;
				for(var j=0;j<gpg_type.length;j++){
					if(gpg_ext_type[i].value == gpg_type[j].value){
						exsits = true;
						break;
					}
				}			
				if(exsits){
					var t_count = document.getElementById('group_pro_goods_type_'+gpg_ext_type[i].value+'_count').value;
					var e_money = document.getElementById('group_pro_g_ext_'+gpg_ext_type[i].value+'_money').value;
					all_count += parseInt(t_count);
					ext_money += parseFloat(e_money);
				}		
			}
		}
		if(arr_g_ids.length<all_count){
			alert('你漏选商品了！');
			return false;			
		}
		var g_idArr = new Array();
		var c_idArr = new Array();
		var s_idArr = new Array();
		for(var i=0;i<arr_g_ids.length;i++){
			if(!Utils.isInt(arr_g_ids[i].value) || !Utils.isInt(arr_c_ids[i].value) || !Utils.isInt(arr_s_ids[i].value)){
				alert('参数丢失，无法提交！');
				return false;
			}else{
				g_idArr.push(arr_g_ids[i].value);
				c_idArr.push(arr_c_ids[i].value);
				s_idArr.push(arr_s_ids[i].value);
			}
		}
		if (g_idArr.length <= 0 || (g_idArr.length != c_idArr.length) || (g_idArr.length != s_idArr.length)){
			alert('参数不正确，无法提交！');
			return false;
		}else{
			var elements = document.forms['theForm'].elements;	
			if(Utils.isNumber(ext_money) && ext_money>0){
				elements['group_pro_g_ext_money'].value = ext_money;	    	
			}
			elements['group_pro_goods_ids'].value = g_idArr.join(',');
			elements['group_pro_color_ids'].value = c_idArr.join(',');
			elements['group_pro_size_ids'].value = s_idArr.join(',');
			return true;
		}
	}
}